package cn.xshi.log.web;

import cn.xshi.log.model.LogOperate;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.log.service.LogOperateService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Desc 操作日志表 
* @Author 邓纯杰
* @CreateTime 2022-08-24 12:27:00
*/
@RestController
@RequestMapping("/logOperate")
@Api(value = "操作日志API",tags = "操作日志API",description = "操作日志")
public class LogOperateController extends BaseAction{

	@Resource
	private LogOperateService logOperateService;

	/**
	* 查询并分页
	* @param baseSearch 
	*/
	@ApiOperation(value="查询并分页", notes="查询并分页")
	@PostMapping(value="/list")
	@Auth(value = "/logOperate/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<LogOperate>> getLogOperateListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<LogOperate> logOperateList = logOperateService.getLogOperateListByCondition(condition);
		for(LogOperate logOperate : logOperateList){
			if(!StringUtil.isEmpty(logOperate.getCreateId())){
				OauthAccountEntity createBy = getAccount(logOperate.getCreateId());
				if(null != createBy){
					logOperate.setCreateBy(createBy.getName());
				}
			}
		}
		PageInfo<LogOperate> page = new PageInfo<LogOperate>(logOperateList);
		return outPageBootStr(page,baseSearch);
	}

	/**
	* 查询单条记录
	* @param id 
	*/
	@ApiOperation(value="查询单条记录", notes="查询单条记录")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/logOperate/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<LogOperate> getLogOperateById(@PathVariable("id")String id){
		LogOperate logOperate = logOperateService.getLogOperateById(id);
		if(!StringUtil.isEmpty(logOperate.getCreateId())){
			OauthAccountEntity createBy = getAccount(logOperate.getCreateId());
			if(null != createBy){
				logOperate.setCreateBy(createBy.getName());
			}
		}
		return outDataStr(logOperate);
	}

	/**
	* 添加
	* @param logOperate 
	*/
	@ApiOperation(value="添加", notes="添加")
	@PostMapping(value="/add")
	@Auth(value = "/logOperate/add",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public BaseResult addLogOperate(@RequestBody LogOperate logOperate){
		int i = 0;
		if(null != logOperate){
			logOperate.setId(toUUID());
			i=logOperateService.addLogOperate(logOperate);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 删除
	* @param id 
	*/
	@ApiOperation(value="删除", notes="删除")
	@DeleteMapping(value="/delete")
	public BaseResult delLogOperate(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i=logOperateService.delLogOperate(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
